<template>
  <div class="dashboard-container">
    <div class="app-container">
      <template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPermission(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格   row-key="id" 转换树形结构要加这个属性  -->
      <el-table :data="list" border row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column  prop="code" align="center" label="标识" />
        <el-table-column prop="description" align="center" label="描述" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <el-button v-if="row.type===1"  type="text"  @click="addPermission(2,row.id)">添加</el-button>
            <el-button type="text" @click="edit(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>
    <!-- 新增编辑的弹层 -->
<el-dialog  @close="btnCancel" :title="showText" :visible="showDialog">
  <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
    <el-form-item prop="name" label="名称">
      <el-input v-model="formData.name" style="width:90%"></el-input>
    </el-form-item>
    <el-form-item prop="code" label="标识">
      <el-input v-model="formData.code" style="width:90%"></el-input>
    </el-form-item>
    <el-form-item label="描述">
      <el-input v-model="formData.description" style="width:90%"></el-input>
    </el-form-item>
    <el-form-item label="开启">
      <!-- 绑定字符串 -->
      <!-- 激活时 是1  不激活时0 -->
      <el-switch
      v-model="formData.enVisible"
      style="width:90%;"
      active-value="1"
      inactive-value="0"
      ></el-switch>
    </el-form-item>
  </el-form>
  <!-- 底部footer -->
  <el-row slot="footer" type="flex" justify="center">
    <el-col :span="6">
  <el-button type="primary" @click="btnOk" size="small">确定</el-button>
  <el-button  size="small" @click="btnCancel">取消</el-button>
    </el-col>
  </el-row>
</el-dialog>
  </div>
</template>
    </div>
  </div>
</template>

<script>
import { getPermissionList,addPermission,updatePermission,delPermission,getPermissionDetail } from '@/api/permisson';

import { tranListToTreeData } from '@/utils';
export default {
data() {
  return {
    list:[],
    showDialog:false,
    formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      },
  }
},
computed:{
showText(){
  return this.formData.id? '编辑权限':'新增权限'
}
},
created() {
  this.getPermissionList()
},
methods: {
 async  getPermissionList(){
  this.list=tranListToTreeData(await getPermissionList(),'0')
 },
//  删除权限
  async delPermission(id){
      try {
        await this.$confirm('确认删除吗？')
        await delPermission(id)
        this.getPermissionList()
        this.$message('删除成功')
      } catch (err) {
        console.log(err);
        this.$message('删除失败')

      }
 },
//  添加按钮 只是存数据 打开弹窗
 addPermission(type,pid){
   this.formData.type=type
   this.formData.pid=pid
   this.showDialog=true
 },
//  确定
async btnOk(){
  // 区分是新增还是编辑 this.forData.id
  if(this.formData.id){
    try {
        await this.$refs.perForm.validate()
        await updatePermission(this.formData)
        this.getPermissionList()
        this.$message.success('编辑成功')
        this.showDialog=false
      } catch (err) {
        console.log(err);
        this.$message('编辑失败')

      }
  }else{
// 新增
try {
        await this.$refs.perForm.validate()
        await addPermission(this.formData)
        this.getPermissionList()
        this.$message.success('添加成功')
        this.showDialog=false
      } catch (err) {
        console.log(err);
        this.$message('添加失败')

      }
  }
  
},
//  取消
 btnCancel(){
  this.formData={}
  this.$refs.perForm.resetFields()//移除校验
this.showDialog=false
 },
//  编辑按钮
async edit(id){
this.formData= await getPermissionDetail(id)

  this.showDialog=true
}

},
}
</script>

<style>

</style>
